home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6481 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Does C have Dictionarys like smalltalk does?
  5. Date: 24 Feb 1996 12:20:11 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gnrtrINN104@keats.ugrad.cs.ubc.ca>
  8. References: <4ged8d$8dv@bertrand.ccs.carleton.ca>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4ged8d$8dv@bertrand.ccs.carleton.ca>,
  12. Andrew Belo <abelo@chat.carleton.ca> wrote:
  13. >
  14. >I am trying to make a program that takes a name and phone number and
  15. >"links" them together and stores the name, number and link in a file.  In
  16. >smalltalk I would be able to use a dictionary that associates 2 objects
  17. >together I was wondering if there were similar structures in C.  My
  18.  
  19. C is not "that kind of language". Dictionaries are typically  not found in 
  20. machine language instruction sets.
  21.  
  22. C is the kind of language in which a Smalltalk compiler or interpreter is
  23. likely to be written. 
  24.  
  25. In C, you can create dictionary data structures yourself. Typically, a
  26. dictionary will be abstracted as the address of a ``struct''---a record which
  27. contains information about that dictionary. What is inside this structure is up
  28. to you. It could be the header record for maintaining a binary tree, hash
  29. table, B+Tree---what have you.
  30.  
  31. The best way is to implement abstract operations in terms of C functions which
  32. create and destroy dictionaries, insert and delete entries and so forth. C does
  33. not directly support polymorphism, so you can't neatly write a data structure
  34. that will hold objects of any type. One way to do it is to have the structure
  35. hold "void pointers", which are generic addresses. The address of any object
  36. regardless of its type can be converted to such a pointer and back.
  37. You can also define "unions",  which are structures that hold muliple fields in
  38. the same space, much like Pascal "variant records". If you make your dictionary
  39. store objects that are unions of a void pointer and an integer, then you can
  40. directly store pointers to any object in your data structure, as well as
  41. integers.
  42.  
  43.  
  44.  
  45. If you want a quick and dirty (albeit a little limited) dictionary facility
  46. using only ANSI standard C library functions, you can use arrays. The C library
  47. provides a function that can sort arrays which contain elements of arbitrary
  48. types --- you just provide a comparison function which tells qsort() whether
  49. one element is greater than another in terms of some ordering criteria.
  50. Another function, bsearch() implements a binary search on a sorted array. 
  51. These two, along with some dynamic array manipulation, can be used to manage
  52. dictionaries of any size. Of course, insertions and deletions are O(n).
  53. Hash tables are much better for this purpose.
  54.  
  55.  
  56. -- 
  57.  
  58.